var Md5 = require('../../utils/md5.js');
var timeSecond = Math.floor(Date.now() * 0.001);  //获取时间戳: 
var SignInfoData = Md5.hexMD5(timeSecond + 'nativetalk')//md5加密;
var httpFile = 'http://file.waijiaojun.com/';
var countdown = 60;
var settime = function (that) {
  if (countdown == 0) {
    that.setData({
      is_show: true
    })
    countdown = 60;
    return;
  } else {
    that.setData({
      is_show: false,
      last_time: countdown
    })
    countdown--;
  }
  setTimeout(function () {
    settime(that)
  }
    , 1000)
};
var phoneNum = '';
var testCode = '';
var password = '';
var passwordSure = '';
var code, userInfo, phoneCode;
const app = getApp();
var httpNativetalk = app.globalData.httpNativetalk;
var httpWeike = app.globalData.httpWeike;
var js_code;
Page({
  data: {
    testCodeData: true,
    phoneNumData: true,
    passwordData: true,
    passWordSureData: true,
    last_time: '',
    is_show: true,
    passwordFlag: true,
  },
  onLoad: function () {
    //获取个人信息
    wx.getUserInfo({
      success: res => {
        userInfo = res.userInfo;
      }
    });
    //获取code
    wx.login({
      success: res => {
        code = res.code;
      }
    })
  },
  //获取手机号
  phoneNumFun: function (e) {
    phoneNum = e.detail.value;  
    // console.log("1手机号为" + phoneNum)    
    if (phoneNum == '') {
      this.setData({
        phoneNumData: false,
      })
    };
    if (phoneNum !== '') {
      this.setData({
        phoneNumData: true,
        tel: phoneNum,
      })
    }
  },
  //
  startFun:function(e){
    console.log("2手机号为" + phoneNum)
  },
  clickVerify: function (e) {
    console.log(e)
    console.log("3手机号为" + phoneNum)
    if (phoneNum !== '') {
      var that = this;
      // 将获取验证码按钮隐藏60s，60s后再次显示
      that.setData({
        is_show: false,  //false
      })     
      //发送验证码的请求
      wx.request({
        url: httpWeike + '/wechatSmallProgram/sendMessage',
        data: {
          "memb_phone": phoneNum,
          "memb_phone_area": 86,
        },
        header: {
          "Timestamp": timeSecond,
          "SignInfo": SignInfoData,
          "content-Type": "application/json",
          "client_type": "3",
          "version": 15,
        },
        method: 'POST',
        success: function (res) {
          console.log(res);
        },
        fail: function (res) { },
        complete: function (res) { },
      });
      settime(that);
    }
  },
  //获取输入密码
  passwordFun: function (e) {
    console.log(e.detail.value.length)
    if (e.detail.value == '' || e.detail.value.length > 12 || e.detail.value.length < 6) {     
      this.setData({
        passwordData: false,
      })
    };
    if (e.detail.value !== '' && e.detail.value.length <= 12 && e.detail.value.length >= 6) {
      password = e.detail.value;     
      this.setData({
        passwordData: true,

      })
    }
  },
  //确认密码输入密码
  passWordSureFun: function (e) {
    passwordSure = e.detail.value;
    // console.log('password=' + password, 'passwordSure=' + passwordSure)
    if (password !== passwordSure) {
      this.setData({
        passWordSureData: false,
      })
    };
    if (password == passwordSure) {
      this.setData({
        passWordSureData: true,
      })
    }
  },
  //获取输入的验证码
  testCodeFun: function (e) {
    testCode = e.detail.value;
    // console.log(testCode);
    if (testCode == '') {
      this.setData({
        testCodeData: false,
      })
    }
    if (testCode !== '') {
      this.setData({
        testCodeData: true,
      })
    }
  },
  //立即绑定
  nowBingFun: function () {
    var that = this;
    // console.log("password=" + password, "passwordSure=" + passwordSure);
    //老用户绑定
    if (phoneNum !== '' && testCode !== '' && password == '' && passwordSure == '') {
      wx.login({
        success: res => {
          js_code = res.code;
          wx.request({
            url: httpWeike + '/wechatSmallProgram/checkUserState',
            data: {
              "memb_phone": phoneNum,
              "memb_phone_area": 86,
              "code": testCode,
              "js_code": js_code,
            },
            header: {
              "Timestamp": timeSecond,
              "SignInfo": SignInfoData,
              "content-Type": "application/json",
              "client_type": "3",
              "version": 15,
            },
            method: 'POST',
            success: function (res) {
              console.log(res);
              if (res.data.tip == '绑定成功') {
                wx.showToast({
                  title: '绑定成功',
                  icon: 'success',
                  duration: 2000
                });
                setTimeout(function () {
                  wx.switchTab({
                    url: '/pages/my/my'
                  });
                }, 2000)
              };
              if (res.data.tip == '查询成功') {
                that.setData({
                  passwordFlag: false,
                })
              };
              if (res.data.mark == '-1') {
                wx.showToast({
                  title: res.data.tip,
                  icon: 'loading',
                  duration: 2000
                });
              }
              if (res.data.mark == '-2') {
                wx.showToast({
                  title: res.data.tip,
                  icon: 'loading',
                  duration: 2000
                });
              }
            },
            fail: function (res) { },
            complete: function (res) { },
          })
        }
      })
    }
    //新用户绑定
    
    if (phoneNum !== '' && testCode !== '' && password!==''&& password == passwordSure) {
      console.log("新用户绑定")
      wx.login({
        success: res => {
          js_code = res.code;
          wx.request({
            url: httpWeike + '/wechatSmallProgram/smallProgram/register',
            data: {
              "phoneCode": testCode,
              "memberPassword": password,
              "memberPhone": phoneNum,
              "memberPhoneArea": "86",
              "memberName": userInfo.nickName,
              "sex": userInfo.gender,
              "memberHeadPortrait": userInfo.avatarUrl,
              "code": js_code,
            },
            header: {
              "Timestamp": timeSecond,
              "SignInfo": SignInfoData,
              "content-Type": "application/json",
              "client_type": "3",
              "version": 15,
            },
            method: 'POST',
            success: function (res) {
              console.log(res);
              // console.log(res.data.obj.token)
              if (res.data.mark == '1') {
                wx.showToast({
                  title: '注册成功',
                  icon: 'success',
                  duration: 2000
                });
                setTimeout(function () {
                  wx.switchTab({
                    url: '/pages/my/my'
                  });
                }, 2000)
              }
              if (res.data.mark == '-1') {
                wx.showToast({
                  title: res.data.tip,
                  icon: 'loading',
                  duration: 2000
                });
              }
            },
            fail: function (res) { },
            complete: function (res) { },
          })
        }
      })

    }
  }
})